{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1587cc8c",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "1、购物车内容详解\n",
    "\n",
    "2、字典的具体使用方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8eed532e",
   "metadata": {},
   "source": [
    "### 购物车程序项目：\n",
    "\n",
    "要求： \n",
    "\n",
    "1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caee33a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 商品列表\n",
    "\n",
    "goods_list = [\n",
    "    # 商品1 商品名称,商品价格,商品编号...\n",
    "    # 商品模板 : [商品名称，商品名称]\n",
    "    [\"珍珠奶茶小杯\",7],\n",
    "    [\"珍珠奶茶大杯\",8.5],\n",
    "    [\"珍珠奶茶加大杯\",9.5],\n",
    "    [\"珍珠奶茶1L\",12],\n",
    "    [\"珍珠奶茶桶装\",20],\n",
    "]\n",
    "\n",
    "# 不是所有的代码都可以一次性考虑全面！例如这里应该写一个用户商品列表\n",
    "user_shopping_cart = [] #append可以新增列表\n",
    "\n",
    "# 用户输入：input\n",
    "Alipay = int(input(\"请输入您的余额：\"))\n",
    "\n",
    "#打印商品列表（规范打印，样式是先写的，然后再输入数据）\n",
    "#先设计模板样式：商品编号：xx，商品名称：xx，商品价格：xx元\n",
    "while True:\n",
    "    print(\"-------【商品列表】-------\")\n",
    "    for i in range(len(goods_list)):\n",
    "        print(\"商品编号：%s,商品名称：%s,商品的价格：%d元\"%(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "    # 2、让用户输入商品编号进行商品的购买\n",
    "    choice = input(\"请输入正确的商品编号（输入“q”可以退出购买）:\") # 用户输入可能不是数值，\n",
    "\n",
    "    if choice.isdigit():\n",
    "        # 用int转换数据类型为数值\n",
    "        choice = int(choice)\n",
    "        # 是正确的编号\n",
    "        if choice < len(goods_list): # 商品的信息：goods_list[choice],商品名称：goods_lisr[choice][0],商品价格：goods_list[choice][1]\n",
    "            #1、看一看用户的钱够不够：Alipay vs goods_list[choice][1]\n",
    "            if Alipay < goods_list[choice][1]:\n",
    "                print(\"您的余额不够，请更换支付方式\")\n",
    "            # 2、如果够的话，赶紧扣钱！把商品加进购物车\n",
    "            else:\n",
    "                Alipay = Alipay - goods_list[choice][1]\n",
    "                user_shopping_cart.append(goods_list[choice])\n",
    "                #提示用户：本次购买成功\n",
    "                print(\"【%s】 已经添加到您的购物车，您的余额还有【%d】元\"%(goods_list[choice][0],Alipay))\n",
    "\n",
    "        # 不是正确的编号\n",
    "        else:\n",
    "            print(\"您输入的商品不存在，请输入正确的编号\")\n",
    "    elif choice == \"q\":\n",
    "        print(\"--------------\",'\\n',\"您的购物车：%s\"%(user_shopping_cart),'\\n',\"您的余额还有：%d 元\"%(Alipay))\n",
    "        break\n",
    "        # 两种情况：\n",
    "        # 1、按照我们的要求输入的文本，例如：输入q表示quit退出的意思\n",
    "        \n",
    "        # 2、输入的不符合我们的要求的文本\n",
    "    else:\n",
    "        print(\"请输入正确的商品编号！或者输入“q”退出\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "670f3c06",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
